package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.AppItem;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.Config;

public class ResetPasswordActivity extends BaseActivity implements
		OnClickListener {
	private TextView mResetPass;
	private Intent mIntent;
	private EditText pw, npw;
	String phone,code;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reset_password);
		initView();
	}

	private void initView() {
		mContext = this;
		mBack = (ImageView) findViewById(R.id.back_btn);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		pw = (EditText) findViewById(R.id.et_pw);
		npw = (EditText) findViewById(R.id.et_npw);
		phone = getIntent().getStringExtra("phone");
		code = getIntent().getStringExtra("code");
		mBack.setOnClickListener(this);
		mTittle.setText("重置密码");
		mResetPass = (TextView) findViewById(R.id.reset_complite);
		mResetPass.setOnClickListener(this);

	}

	/**
	 * 
	 */
	private void complete() {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(pw.getText().toString())) {
			Toast.makeText(mContext, "密码不能为空", 2).show();
			return;
		} else if (TextUtils.isEmpty(npw.getText().toString())) {
			Toast.makeText(mContext, "请再次输入密码", 2).show();
			return;
		} else if (!pw.getText().toString().equals(npw.getText().toString())) {
			Toast.makeText(mContext, "两次输入密码不一致", 2).show();
			return;
		}
		VolleyBasic.getInstance(ResetPasswordActivity.this).post(Cache.resetPwdData,
				VolleyMap.getInstance().resetPwd(pw.getText().toString(), npw.getText().toString(), code, phone), AppItem.class, new VolleyCallback() {
					
					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						Config.showTip(mContext, "密码重置成功！");
						finish();
					}
					
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.reset_complite:
			complete();
			break;
		case R.id.back_btn:
			finish();
		default:
			break;
		}

	}
//	VolleyRequest stringRequest = new VolleyRequest(Request.Method.POST,
//			Cache.resetPwdData,
//			// "http://124.205.21.162:89/m.php/login/Registered/mobile/18710085136/password/abc765/realname/huwei222",
//			new Listener<String>() {
//
//				@Override
//				public void onResponse(String s) {
//					progressDialog.dismiss();
//					VolleyData VD = new VolleyData(s);
//					if (!TextUtils.isEmpty(VD.getJson())) {
//						System.out.println(VD.getJson());
//					}
//					if (!TextUtils.isEmpty(VD.getMsg())) {
//						Config.showTip(mContext, VD.getMsg());
//					}else{
//						Config.showTip(mContext, "密码重置成功！");
//					}
//					if (VD.getCode() != BasicParser.Success) {
//						return;
//					}
//					finish();
//
//				}
//			}, new ErrorListener() {
//				@Override
//				public void onErrorResponse(VolleyError volleyError) {
//					progressDialog.dismiss();
//					Log.e("volleyerror", "erro2");
//				}
//			}) {
//		@Override
//		protected Map<String, String> getParams() {
//			// 在这里设置需要post的参数
//			Map<String, String> map = new HashMap<String, String>();
//			map.put("mobile", phone);
//			map.put("password", pw.getText().toString());
//			map.put("repassword", pw.getText().toString());
//			map.put("code", code);
//			return map;
//		}
//	};
}
